package ProjectXXX;

import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.sql.*;
import module4.*;

public class PasswordXXX extends JDialog implements ActionListener
{
	//attributes
	private	JPanel northP = null ,southP = null,centerP = null,baseP = null;
	private	JLabel useridL = null, passwordL = null;
	private	JPasswordField passwordJTF = null;
	private	JButton submitBut = null , changePswBut = null,cancelBut = null;
	private JFrame frame = null;
	private String userId = null;
	private String psw = null;
	
	//actionPerformed Attributes
	private boolean foundPassword = false;
	private int accessLevel = 0;
	private String empId = null,initials = null,fName = null,lName = null;
	private String inPassword = null;
	
	//Connection Stuff
	private String myDriver = "com.mysql.jdbc.Driver";
	private String ipOfDB = "localhost";
	private MySQLdriverConnection msc;
	private Connection turtleConn = null;
	
	
	//nitce the contructor taking in a refernace to a parent component
	
	public PasswordXXX(JFrame frame,String msg)
	{
		super(frame); //creates hard link between dialog and frame passed in constructor
                      //calls constructor from super class
        		
        try
		{
			msc = new MySQLdriverConnection("","",ipOfDB,"turtleData");
			turtleConn = msc.getConnection();
		}
		catch(SQLException sqlE)
		{
			sqlE.printStackTrace();
		}	
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}		
		//this.frame = frame;  //instead....
		//setup North panel
		northP = new JPanel(new GridLayout(1,1));
		passwordL = new JLabel(msg);
		passwordJTF = new JPasswordField(10);
		passwordJTF.setBorder(BorderFactory.createEtchedBorder(Color.white,Color.black));   
		passwordJTF.setMaximumSize(new Dimension(200,20));
		passwordJTF.setPreferredSize(new Dimension(200,20));
		passwordJTF.setBorder(BorderFactory.createLoweredBevelBorder());
		passwordJTF.addActionListener(this);
		//System.out.println(passwordJTF.getPassword());
		//String pTest = new String(passwordJTF.getPassword());
		
		northP.add(passwordJTF);
		northP.setBorder(BorderFactory.createCompoundBorder(
			                  BorderFactory.createEmptyBorder(8,4,4,4),BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(Color.white,Color.black),"")));
		
		centerP = new JPanel(new GridLayout(1,1));
		centerP.add(passwordL);
		
		//setup south panel
		southP = new JPanel();
		submitBut = new JButton(new ImageIcon("C:/javausers/IConImages/confirm.gif"));
		submitBut.setBackground(new Color(238,238,238));
		submitBut.setFocusPainted(false);
		submitBut.setBorderPainted(false);
		submitBut.addActionListener(this);
		cancelBut = new JButton(new ImageIcon("C:/javausers/IconImages/cancell.gif"));
		cancelBut.addActionListener(this);
		cancelBut.setBackground(new Color(238,238,238));
		cancelBut.setFocusPainted(false);
		cancelBut.setBorderPainted(false);
		southP.add(submitBut);
		southP.add(cancelBut);
		
		baseP = new JPanel(new BorderLayout());
		baseP.setBorder(BorderFactory.createLineBorder(Color.gray,2));
		baseP.add(northP,BorderLayout.NORTH);
		baseP.add(centerP,BorderLayout.CENTER);
		baseP.add(southP,BorderLayout.SOUTH);
		
		MyWindowListener mw = new MyWindowListener();
		this.addWindowListener(mw);
		this.setLayout(new GridLayout(1,1));
		this.setTitle("password Dialog");
		this.setModal(true);
		this.setSize(400,170);
		this.setLocationRelativeTo(null);
		this.getContentPane().add(baseP);
		
		//this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		//this.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
		this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		this.setVisible(true);
	
	}//end
	
	
	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource(); 
		if(source == cancelBut)
		{
			this.dispose();
		}
		
		if(source == submitBut)
		{	
			  try
			  {
			    
					  inPassword = new String(passwordJTF.getPassword());
					  
					  Statement passSTMT = turtleConn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
					  ResultSet passRS;
					  String passQuery = "SELECT empPassword,empId,accessLevel,initials,name,surname From employeeTable";
					  
					  passRS = passSTMT.executeQuery(passQuery);
					  
					  passRS.last();
					  int amtRows = passRS.getRow();
					  
					  foundPassword = false;
					  passRS.first();
					  
					  for(int i = 1;i <= amtRows;i++)
					  {
					  	  if(foundPassword == true)
					      break;
					      
					      if(inPassword.equals(passRS.getString(1)))
					      {
					      	   foundPassword = true;
					      	   empId = passRS.getString(2);
					      	   accessLevel = Integer.parseInt(passRS.getString(3));
					      	   initials = passRS.getString(4);
					      	   fName = passRS.getString(5);
					      	   lName = passRS.getString(6);
					      	   this.dispose();
					      }
					      passRS.next();
					  }
					  if(foundPassword == false)
					  {
					  	JOptionPane.showMessageDialog(this,"No employee found with given Password.");
					  }
					  
					  
				}
				catch(SQLException sqlE)
				{
					sqlE.printStackTrace();
				}	  
		}
		if(source == passwordJTF)
		{
			submitBut.requestFocus();
		}
	}	
	
	public boolean getLogonSuccess()
	{
		return foundPassword;
	}
	public String getEmpId()
	{
		return empId;
	}
	public String getFirstAndLastName()
	{
		String concatenatedNames = new String(fName +" " +lName);
		return concatenatedNames;
	}
	public String getInitials()
	{
		return initials;
	}
	public int getAccessLevel()
	{
		return accessLevel;
	}
	public void removeDialog()
	{
		this.dispose();
	}
	
	class MyWindowListener extends WindowAdapter
	{
		public void windowClosing(WindowEvent we)
		{
			removeDialog();
		}
	}
	
}//end of class delcaration